/**
 * 时时彩的共用逻辑
 */

// 标题
initGame(game, function() {
    // 填充tab页
    fill_tabs(['两面', '1-5球', '前中后']);

    fill_table();
}, true);

function fill_table() {
    var types = ["大", "小", "单", "双"];
    for (var i = 0; i < types.length; i++) {
        var t = types[i];
        add_bet_item('和' + t, '总和' + t, $(".zonghe"));
    }
    var types = ["龙", "虎", "和"];
    for (var i = 0; i < types.length; i++) {
        var t = types[i];
        add_bet_item(t, t, $(".longhu"));
    }

    // 5球的大小单双
    var ths = ["一", "二", "三", "四", "五"];
    var types = ["大", "小", "单", "双"];
    var div = $(".page1 .five");
    div.empty();
    for (var j = 0; j < ths.length; j++) {
        add_bet_th("第" + ths[j] + "球", div);

        var ul = $('<ul class="am-avg-sm-2 bet"></ul>');
        for (var i = 0; i < types.length; i++) {
            var t = types[i];
            add_bet_item(ths[j] + t, t, ul);
        }
        div.append(ul);
    }

    // 5球的数字
    var div = $(".page2 .five");
    div.empty();
    for (var j = 0; j < ths.length; j++) {
        add_bet_th("第" + ths[j] + "球", div);

        var ul = $('<ul class="am-avg-sm-2 bet"></ul>');
        for (var i = 0; i < 10; i++) {
            add_bet_item(ths[j] + i, '<img src="../public/all/images/__n__.png"/>'.replace(/__n__/, i), ul);
        }
        div.append(ul);
    }

    // 前中后
    var ths = ["前", "中", "后"];
    var types = ['豹子', '顺子', '对子', '半顺'];
    var div = $(".qzh");
    div.empty();
    for (var j in ths) {
        add_bet_th(ths[j] + "三", div);

        var ul = $('<ul class="am-avg-sm-2 bet"></ul>');
        for (var i in types) {
            add_bet_item(ths[j] + types[i][0], types[i], ul);
        }
        div.append(ul);

        var ul = $('<ul class="am-avg-sm-1 bet"></ul>');
        add_bet_item(ths[j] + "杂", "杂六", ul);
        div.append(ul);
    }
}

window.lastest_result2 = function() {
    var c = Result.last();
    if (!c || !c.red) return [];
    var red = c.red;
    var r = [];

    var he = gameRule.he(red);
    r.push(he);

    if (gameRule.isHeBig(red)) r.push('大');
    else r.push('小');
    if (gameRule.isHeOdd(red)) r.push('单');
    else r.push('双');

    if (gameRule.isLong(red)) 
        r.push('龙');
    else if (gameRule.isHu(red))
        r.push('虎');
    else if (gameRule.isHe(red))
        r.push('和');

    return r;
}

// 检测能否下注，看下单号1-10是否满足要求
window.customCheckBetList = function(type, issue, data) {
    if (type != 'cqssc' && type != 'xjssc' && type != 'tjssc' && type != 'zyssc') return true;

    var limit = Util.getNumberLimit(type);
    if (limit <= 0 || limit >= 10) return true;

    var arr1 = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十'];
    var arr2 = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
    var m = {};
    for (var k in data) {
        if (k.length != 2) continue;
        if (arr1.indexOf(k[0]) != -1 && arr2.indexOf(k[1]) != -1) {
            if (!m[k[0]]) m[k[0]] = {};
            m[k[0]][k[1]] = true;
        }
    }
    if (Util.isEmpty(m)) return true;

    // 获取下已经下注的单据
    var bets = context.bets[type];
    if (bets) bets = bets[issue] || [];
    else bets = [];
    for (var i in bets) {
        var k = bets[i].target;
        if (k.length != 2) continue;
        if (arr1.indexOf(k[0]) != -1 && arr2.indexOf(k[1]) != -1) {
            if (!m[k[0]]) m[k[0]] = {};
            m[k[0]][k[1]] = true;
        }
    }

    var total = 0;

    for (var k in m) {
        var m2 = m[k];
        if (Object.keys(m2).length > limit) {
            var l = "下注失败，__g__ 定位玩法，单个名次不允许下注__v__码或者__v__以上！如有疑问，请咨询客服！";
            l = l.replace(/__g__/, get_game_display_name(type));
            l = l.replace(/__v__/g, limit);
            alert2(l, true);
            return false;
        }
    }

    return true;
}